import express from 'express'
export const router = express.Router()
import store from '../../store/Merchant.js'
//持久化存储
import PersistStore from '../../store/PersistentStorage.js'

//封装获取持久化存储
const usegetStoreArr = async () => await PersistStore.getStoreData()

import { createConnectionPool } from '../../mysql/index.js'

let connection = await createConnectionPool()

//获取商户用户列表
router.get('/merchantApi/GetMerchantAccount', async (req, res) => {
  const DataArr = await store.getMerchantAccount()

  if (DataArr instanceof Array) {
    res.status(200).send({ code: 1, message: '获取数据成功', data: DataArr })
  } else res.status(400).send({ code: 400, message: `获取数据失败` })
})

// //添加商户用户列表
// router.post('/merchantApi/AddMerchantAccount', async (req, res) => {
//   const MerchantInfo = await store.getMerchantAccount()

//   // 错误是undefind 成功是当前对象
//   const isMerchant = MerchantInfo.find(
//     user =>
//       user.userName === req.body.userName &&
//       user.password === req.body.password &&
//       user.Acode === req.body.Acode &&
//       user.is == req.body.is
//   )
//   console.log(`JSON文件内数据`, MerchantInfo)
//   console.log(`用户登录成功后账号M密码`, req.body.id === MerchantInfo.id)

//   if (req.body.iF == 0) {
//     //删除不添加到对象内
//     delete req.body.iF
//     const str = await store.AddMerchantAccount(req.body)
//     if (str == '添加成功') {
//       return res.status(200).send({ code: 1, message: '提交成功' })
//     } else {
//       return res.status(400).send({ code: 0, message: '提交失败' })
//     }
//   }

//   if (!isMerchant)
//     return res.status(400).send({ code: 400, message: '账号密码错误' })

//   const usePersistStore = async () => {
//     const store = await PersistStore.addStoreData(req.body)
//     console.log(`用户输入数据`, store)
//   }
//   //获取
//   const getStoreArr = await usegetStoreArr()
//   //添加持久化数据
//   const index = getStoreArr.some(item =>
//     item.userName.includes(req.body.userName)
//   )
//   console.log(`indexOF`, index)

//   //如果添加的账号不存在我就执行
//   if (!index) return usePersistStore()
//   //持久化存储结束
//   return res.status(200).send({ code: 1, data: req.body, message: '登录成功' })
// })




//添加商户用户列表
router.post('/merchantApi/AddMerchantAccount', async (req, res) => {

  const { userName, password } = req.body;

  try {
    // 从数据库中获取用户信息
    const [rows] = await connection.query('SELECT * FROM user WHERE status = 0');
    console.log("rows", rows);
    // 查找符合条件的用户
    const isMerchant = rows.find(user => user.username == userName && user.password == password);
    console.log("userName, password", userName, password);
    if (!isMerchant) {
      return res.status(400).send({ code: 400, message: '账号密码错误' })
    } else if (isMerchant.user_state == 0 || !isMerchant.user_state) {
      return res.status(400).send({ code: 401, message: '账号已禁用' });
    } else {
      return res.status(200).send({ code: 1, message: '登录成功' });
    }
  } catch (error) {
    console.error('数据库查询错误:', error);
    return res.status(500).send({ code: 0, message: '服务器内部错误' });
  }

})














//删除持久化存储数据
router.delete('/merchantApi/DeleteMerchantAccount', async (req, res) => {
  console.log(`查看我的`, req.body.id)
  let str = await PersistStore.dekStoreData(req.body.id)
  if (str == '删除成功') {
    res.status(200).send({ code: 1, message: '删除成功' })
  } else {
    res.status(400).send({ code: 400, message: '删除失败' })
  }
})

//删除商户用户列表
router.delete(
  '/merchantApi/DeleteMerchantpersistentStorage',
  async (req, res) => {
    console.log(`查看我的`, req.body.id)
    let str = await store.delMerchantAccount(req.body.id)
    if (str == '删除成功') {
      res.status(200).send({ code: 1, message: '删除成功' })
    } else {
      res.status(400).send({ code: 400, message: '删除失败' })
    }
  }
)

//获取持久化存储数据
router.get('/merchantApi/GetHomeUserInfo', async (req, res) => {

  if (!req.query.userName) {
    res.status(400).send({ code: 400, message: `userName必传` })
    return
  }

  const DataArr = await store.getMerchantAccount()
  const indexList = DataArr.find(item => item.userName == req.query.userName)
  console.log(`用户的数据`, indexList)
  if (DataArr instanceof Array && indexList) {
    res
      .status(200)
      .send({ code: 1, message: '获取数据成功', data: indexList })
  } else res.status(400).send({ code: 400, message: `获取数据失败` })

})

//修改商户余额
router.put('/PutUserBalance/PutUserInfo', async (req, res) => {
  let str = await store.PutMerchantAccount(req.body.obj)
  if (str === '修改成功') res.status(200).send({ code: 1, message: '修改成功' })
  else res.status(400).send({ code: 400, message: '修改失败' })
})
export default router
